<?php

class StudentQuery_KindController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $year = $this->view->Semester['year'];
        $semester = $this->view->Semester['semester'];
        $q = Doctrine_Query::create()
        ->select('a.sex, COUNT(a.sn) AS cc, b.grade AS grade, c.kind AS kind')
        ->from('Student_Model_Student a')
        ->leftJoin('a.Student_Model_StudentSemester b')
        ->leftJoin('a.Student_Model_StudentCharacter c')
        ->whereIN('a.study_condition',
        array(Student_Model_StudentTransfer::IN_STADY,
        Student_Model_StudentTransfer::HOMESCHOOLING))
        ->andWhere('b.year=?', $year)
        ->andWhere('b.semester=?', $semester)
        ->groupBy('a.sex, b.grade, c.kind')
        ->orderBy('c.kind');


        $arr = array();

        foreach ($q->fetchArray() as $row) {
            $arr[$row['kind']][$row['grade']][$row['sex']] = $row['cc'];
        }
        $this->view->data = $arr;
    }

    /**
     * 列出學生資料
     * @return unknown_type
     */
    function getDetailAction()
    {
        $this->getHelper('layout')->disableLayout();
        $id = $this->_getParam('id');
        $arr = explode("-",$id);
        $year = $this->view->Semester['year'];
        $semester = $this->view->Semester['semester'];
        $q = Doctrine_Query::create()
        ->select('a.name, a.identification_number, a.sex, a.birthday,
        a.address, a.phone_home, b.grade AS grade, b.class_sn AS class_sn, b.number AS number,
        b.student_id AS student_id, c.kind AS kind, d.name ,d.sex, e.name as guardian_name')
        ->from('Student_Model_Student a')
        ->leftJoin('a.Student_Model_StudentSemester b')
        ->leftJoin('a.Student_Model_StudentCharacter c')
        ->leftJoin('a.Student_Model_StudentParents d')
        ->leftJoin('a.Student_Model_StudentGuardian e')
        ->whereIN('a.study_condition',
        array(Student_Model_StudentTransfer::IN_STADY,
        Student_Model_StudentTransfer::HOMESCHOOLING))
        ->andWhere('b.year=?', $year)
        ->andWhere('b.semester=?', $semester)
        ->andWhere('c.kind=?',$arr[0])
        ->orderBy('b.grade, b.class_sn, b.number, d.sex');
        $this->view->data = $q->fetchArray();
        if ($this->_getParam('type') == 'csv') {
            $this->getResponse()->setHeader("Pragma","public");
            $this->getResponse()->setHeader("Expires", 0);
            $this->getResponse()->setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
            $this->getResponse()->setHeader("Cache-Control: ","private",false);
            $this->getResponse()->setHeader("Content-Type",'text/csv');
            $this->getResponse()->setHeader("Content-Disposition","attachment; filename=\"student.csv\";");
            $this->render('csv');
        }
        else
        $this->render('get-detail');
    }
}

